package org.testis.data.parser;

import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.DynamicMapParameter;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.testdriver.AtofEnumaration;
import org.testis.testdriver.TestPlanGlobalsHashMap;
import org.testis.testdriver.TestStepGlobalDataHashMap;
import org.testis.testdriver.translation.AtofValueTranslateService;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.testis.webautomationmodule.ui.UIEnumaration.UIObjectParameter;
import org.testis.webautomationmodule.ui.UIEnumaration.UIObjectType;

/**
 *
 * @author nurhat
 */
public class WAMPageContentVerificationDopDecorator extends WAMDopDecorator {

    public WAMPageContentVerificationDopDecorator(
            WebAutomationModuleDataObject data,
            DynamicMapDesignTreeNode root) {
        super(data,
                root);
    }

    @Override
    protected boolean setBehavior() {
        String pageId = null;
        String abstract_link_id = null;
        if (getData().getDataToApply().containsKey(
                AtofModuleParameterNames.WEB_AUTO_PAGE_ID)) {
            pageId = getData().getDataToApply().get(
                    AtofModuleParameterNames.WEB_AUTO_PAGE_ID);
            if (pageId.equals("")) {
                pageId = null;
            }
        }
        if (getData().getDataToApply().containsKey(
                AtofModuleParameterNames.WEB_AUTO_ABSTRACT_LINK_ID)) {
            abstract_link_id = getData().getDataToApply().get(
                    AtofModuleParameterNames.WEB_AUTO_ABSTRACT_LINK_ID);
            if (abstract_link_id.equals("")) {
                abstract_link_id = null;
            }
        }
        if (pageId == null) {
            LogSenderService.putMessageOnQueue(
                    "WebAutomationDataObjectParser",
                    AtofLogLevel.ERROR,
                    "No  page id  parameter given for PageContentVerificationAction",
                    getData().getMessageId());
            return false;
        }

        DynamicMapDesignTreeNode pageNode = findObject(pageId);
        if (pageNode == null) {
            LogSenderService.putMessageOnQueue(
                    "WebAutomationDataObjectParser",
                    AtofLogLevel.ERROR,
                    "No  page found(" + pageId
                    + ") for PageContentVerificationAction",
                    getData().getMessageId());
            return false;
        }


        DynamicMapDesignTreeNode[] childs = pageNode.getChildren();
        getData().getDataToApply().clear();
        getData().setActionType(AtofEnumaration.AtofWebAutomationModuleAction.GetAndVerify.name());
        if (abstract_link_id != null) {
            getData().
                    getDataToApply().put(abstract_link_id,
                    "");
        }
        boolean result = true;
        try {
            for (int i = 0; i < childs.length; i++) {
                if (childs[i].getObject().getTypeName().equals(UIObjectType.UILabel.name())) {
                    DynamicMapParameter parAbstractId =
                            childs[i].getObject().
                            getParameterByName(UIObjectParameter.AbstractId.name());

                    setLanguageObject(parAbstractId,
                            childs[i]);


                }
            }
        } catch (Exception ex) {
            result = false;
        }
        return result;
    }

    private void setLanguageObject(DynamicMapParameter parAbstractId,
            DynamicMapDesignTreeNode node) throws
            Exception {

        if (parAbstractId == null) {
            LogSenderService.putMessageOnQueue("WebAutomationDataObjectParser",
                    AtofLogLevel.ERROR,
                    "No  abstract id parameter found for ( "
                    + node.getObject().toString()
                    + ") at PageContentVerificationAction",
                    getData().getMessageId());
            return;
        }
        String abstractId = getParameterValue(parAbstractId);
        DynamicMapParameter parLanguage =
                node.getObject().getParameterByName(
                UIObjectParameter.InnerHTML.name());
        if (parLanguage != null) {
            String languageVal = getParameterValue(parLanguage);
            if (!languageVal.equals("")) {
                String translatedVal;
                try {
                    AtofValueTranslateService service = new AtofValueTranslateService(
                            TestPlanGlobalsHashMap.createInstance().getTestplanglobals(),
                            TestPlanGlobalsHashMap.createInstance().getTestplanlanguages(),
                            TestPlanGlobalsHashMap.createInstance().getTestplandutvalues(),
                            TestStepGlobalDataHashMap.createInstance().getTeststepglobals());
                    translatedVal = service.translateValue(
                            languageVal);
                } catch (Exception ex) {
                    LogSenderService.putMessageOnQueue(
                            "WebAutomationDataObjectParser",
                            AtofLogLevel.ERROR,
                            languageVal
                            + " not found in global or language variables. Error:"
                            + ex.getMessage(),
                            getData().getMessageId());
                    throw ex;
                }
                if (translatedVal != null) {
                    getData().
                            getDataToApply().put(abstractId,
                            translatedVal);
                } else {
                    getData().
                            getDataToApply().put(abstractId,
                            languageVal);
                }
            }
            /* else {
            getData().
            getDataToApply().put(abstractId,
            "");
            }*/
        } else {
            LogSenderService.putMessageOnQueue(
                    "WebAutomationDataObjectParser",
                    AtofLogLevel.ERROR,
                    "No  innerHTML parameter found for ( "
                    + abstractId + ") at PageContentVerificationAction",
                    getData().getMessageId());
        }

    }
}
